using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.ComponentModel;
using C4F.DevKit.WebServices.Properties;
using System.Globalization;
using C4F.DevKit.WebServices.Datasets;

namespace C4F.DevKit.WebServices
{
    /// <summary>
    /// Exposes 'get popular stories' functionality on Digg.com
    /// </summary>
    [ToolboxItem(true)]
    public class Digg : C4FWebServicesBase
    {
        #region Private Members
        /// <summary>
        /// Request Url
        /// </summary>
        private static string baseUrl = Resources.DiggBaseUrl;
        #endregion

        #region Public Functions
        /// <summary>
        /// Gets Popular story headlines
        /// </summary>
        /// <returns>List of stories returned</returns>
        public List<DiggStory> GetHeadlines()
        {
            // Get the responses
            string responseData = base.GetQueryResponse(baseUrl);

            // Parse the data and return results
            return Digg.ParseResponseXml(responseData);
        }

        /// <summary>
        /// Gets Popular story headlines and returns the result in a dataset
        /// </summary>
        /// <returns>Dataset containing list of popular stories</returns>
        public DS_Digg GetHeadlines_DS()
        {
            List<DiggStory> stories = GetHeadlines();
            DS_Digg dataset = new DS_Digg();
            foreach (DiggStory story in stories)
            {
                dataset.DiggStory.AddDiggStoryRow(story.Title, story.Date, story.Description, story.Diggs, story.Url, story.Topic, story.TopicContainer);
            }
            return dataset;
        }
        #endregion

        #region Private Functions
        /// <summary>
        /// Parses the responseData and creates 'storyList' by populating DiggStory objects
        /// </summary>
        /// <param name="responseData">Query response data</param>
        /// <returns>List of Digg Stories</returns>
        private static List<DiggStory> ParseResponseXml(string responseData)
        {
            List<DiggStory> storyList = new List<DiggStory>();

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(responseData);
            XmlNodeList storyNodeList = xmlDocument.SelectNodes(@"stories/story");
            DiggStory story = null;
            foreach (XmlNode storyNode in storyNodeList)
            {
                try
                {
                    //Populate DiggStory object
                    story = new DiggStory();
                    story.Url = storyNode.Attributes["link"].Value;
                    story.Diggs = Int32.Parse(storyNode.Attributes["diggs"].Value, CultureInfo.InvariantCulture);
                    // Set a base date to enable conversion of Unix timestamps returned in xml to 
                    // DateTime object
                    System.DateTime baseDateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
                    story.Date = baseDateTime.AddSeconds(
                        Convert.ToDouble(storyNode.Attributes["submit_date"].Value, CultureInfo.InvariantCulture));

                    story.Title = storyNode.SelectSingleNode("title").InnerText;
                    story.Description = storyNode.SelectSingleNode("description").InnerText;
                    story.Topic = storyNode.SelectSingleNode("topic").Attributes["name"].Value;
                    story.TopicContainer = storyNode.SelectSingleNode("container").Attributes["name"].Value;
                    //Add populated object to the list
                    storyList.Add(story);
                }
                catch (Exception ex)
                {
                    //log the error, skip this story.
                }
            }
            return storyList;
        }
        #endregion
    }

    /// <summary>
    /// Represents a story Digg.com 
    /// </summary>
    public class DiggStory
    {
        #region Private Members
        /// <summary>
        /// Title of the story
        /// </summary>
        private string title;
        /// <summary>
        /// Submit Date
        /// </summary>
        private DateTime date;
        /// <summary>
        /// Description of the story 
        /// </summary>
        private string description;
        /// <summary>
        /// No. of diggs
        /// </summary>
        private int diggs;
        /// <summary>
        /// Url with the details about the story
        /// </summary>
        private string url;
        /// <summary>
        /// Story Sub Topic
        /// </summary>
        private string topic;
        /// <summary>
        /// Story Main Topic
        /// </summary>
        private string topicContainer;
        #endregion

        #region Public Properties
        /// <summary>
        /// Title of the story
        /// </summary>
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }
        /// <summary>
        /// Date when the story was submitted
        /// </summary>
        public DateTime Date
        {
            get { return this.date; }
            set { this.date = value; }
        }
        /// <summary>
        /// Description of the story 
        /// </summary>
        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }
        /// <summary>
        /// No. of diggs
        /// </summary>
        public int Diggs
        {
            get { return this.diggs; }
            set { this.diggs = value; }
        }
        /// <summary>
        /// Url with the details about the story
        /// </summary>
        public string Url
        {
            get { return this.url; }
            set { this.url = value; }
        }
        /// <summary>
        /// Story Sub Topic
        /// </summary>
        public string Topic
        {
            get { return this.topic; }
            set { this.topic = value; }
        }
        /// <summary>
        /// Story Main Topic
        /// </summary>
        public string TopicContainer
        {
            get { return this.topicContainer; }
            set { this.topicContainer = value; }
        }
        #endregion
    }
}